#!/bin/sh

# The environment variable $SRCHOME cannot be set during the installation
unset SRCHOME

# The environment variable $LD_LIBRARY_PATH should be cleared initially 
unset LD_LIBRARY_PATH

AWK=/bin/awk
SED=/bin/sed
UNAME=/bin/uname
MKDIR=/bin/mkdir
RM=/bin/rm
CP=/bin/cp
MV=/bin/mv
GETCONF=/usr/bin/getconf
LOCALONLY=local_only
LOCAL=TRUE
BUNDLE=compcd

CMDDIR=`dirname $0`

if [ "$CMDDIR" = "." ]; then
    CMDDIR=`pwd`;
fi

# Replace relative path with fully qualified path.
if [ ! "`echo $CMDDIR|grep '^/'`" ]; then
    CMDDIR=`pwd`/$CMDDIR;
fi

case `$UNAME` in
SunOS | HP-UX)
   OCR_LOC=/var/opt/oracle/ocr.loc
;;
*)
   OCR_LOC=/etc/oracle/ocr.loc
;;
esac

# Determine whether there is an existing CRS or not.
# Local is value of local_only in ocr.loc.
# False means there is an existing CRS installation on the system.
if [ -r $OCR_LOC ]; then
    LOCAL=`$AWK -F= /$LOCALONLY/'{print $2}' $OCR_LOC |\
           $SED 'y/trufalse/TRUFALSE/'`
fi

SH_ARCH=64

# INST_MACHINE_ARCH corresponds to Installation machine architecture - whether it is 32-bit or 64-bit h/w
INST_MACHINE_ARCH=`$GETCONF LONG_BIT`

if [ `$UNAME` = "Linux" ]; then
    if [ $SH_ARCH -eq 64 ] && [ $INST_MACHINE_ARCH -ne 64 ]; then
        echo "\"You are attempting to install 64-bit Oracle on a 32-bit operating system.  This is not supported and will not work.\"";
        exit 0;
    fi
fi

SILENT=FALSE

# handle $PATH with blank space problem -bug 11848376
if [ "x${PATH}" != "x" ]; then
    PATH=/usr/bin:/usr/ccs/bin:$PATH:$CMDDIR/install
    export PATH
else
    PATH=/usr/bin:/usr/ccs/bin:$CMDDIR/install
    export PATH
fi

CWDDIR=`pwd`;

#
# Abort the install, if install/.oui file is not found in the stage area
#
if [ ! -f $CMDDIR/install/.oui ]
then
	echo "Source location is incomplete. Make sure you have downloaded and extracted
all the relevant archives."
        exit 1
fi

#
# Loop through the command line argumenets passed to runInstaller script and
# set the MODE variable value to deinstall which is further used to 
# launch the deinstall tool, # if "-deinstall" flag is passed.
#
for i in $*
do
	if [ $i = "-deinstall" ] && [ $BUNDLE = "db" -o $BUNDLE = "client" -o $BUNDLE = "crs" -o $BUNDLE = "tg" -o $BUNDLE = "gsm" ] && [ $BUNDLE != "compcd" ]
	then
		MODE=deinstall
	fi
done

#
# If MODE value is deinstall, then determine/set the BOOSTRAP_DIR location which is 
# used to bootstrap the deconfig componets specified in oraparam.ini.deinstall with OUI.
# After the components are bootstrapped onto BOOTSTRAP_DIR location, deinstall 
# tool will be launched from the same location by passing to the command line
# arguments which are passed originally to runInstaller.
# 

if [ "$MODE" = "deinstall" ];
then

# gsm OH can be deinstalled from gsm shiphome only, because it follows oui deinstall model
# Any other OH (db, client, crs, tg) can be deinstalled from non-gsm shiphome, because it follows 
# the plugin model
# It will display the error message in the following cases:
# Case 1: Trying to deinstall gsm OH from non-gsm shiphome
# Case 2: Trying to deinstall non-gsm OH from gsm shiphome

# Get the ORACLE_HOME from the command line argument
ORACLE_HOME=""
counter=1
for i in $*
do
      if [ $i = "-home" ]
      then
         counter=`expr $counter + 1`
         eval ORACLE_HOME=\$$counter
         break
      fi
         counter=`expr $counter + 1`
done
# if BUNDLE is "gsm" and OH is gsm, then allow OUI based deinstall
if [ $BUNDLE = "gsm" ]; then
     if [! -d "$ORACLE_HOME"/inventory/Components21/oracle.dslm ]; then
# if BUNDLE is "gsm" and OH is non-gsm, then display the error
          echo "The path specified for Oracle_Home $ORACLE_HOME is not $BUNDLE installation. Use appropriate shiphome to deinstall."
          exit 0
     fi
else
# if BUNDLE is "db", "client", "crs", "client" (non-gsm), and if OH is gsm, then display the error
     if [ -d "$ORACLE_HOME"/inventory/Components21/oracle.dslm ]; then
          echo "The path specified for Oracle_Home $ORACLE_HOME is gsm installation. Use gsm shiphome to deinstall."
          exit 0
     fi
fi

	if [ "X$TMPDIR" = "X" ]
	then
		TMPDIR=/tmp
		BOOTSTRAP_DIR=$TMPDIR/deinstall_bootstrap
	else
		BOOTSTRAP_DIR=$TMPDIR/deinstall_bootstrap
	fi
	export BOOTSTRAP_DIR

	# Remove the contents of $BOOTSTRAP_DIR,if it exists already,
	# else create the directory.

	if [ -d $BOOTSTRAP_DIR ];
	then
		$RM -fr $BOOTSTRAP_DIR/*
	else
		$MKDIR -p $BOOTSTRAP_DIR
	fi
        # Check whether /tmp has 425 MB free space
        # Required for bootstrapping for deinstall
        echo "Checking for required space in $TMPDIR directory ..."
        echo "Please wait ..."

        freespace=`df -k $TMPDIR | tail -1 | awk '{print $4}'`
        if [ $freespace -lt 435200 ]
        then
           echo "No sufficient space available on $TMPDIR"
           echo "Please ensure you have atleast 425MB on $TMPDIR before proceeding to deinstall"
           exit 1
        fi
        echo  "Space check on $TMPDIR directory passed..."	

	echo -n "Bootstrapping the deinstall components..."
	$CMDDIR/install/.oui -paramFile $CMDDIR/install/oraparam.ini.deinstall -bootstraponly BOOTSTRAP_LOC=$BOOTSTRAP_DIR > $BOOTSTRAP_DIR/deinstallbootstrap.log
	echo "Done"
	OUI_EXEC_STATUS=$?

	#
	# If we encountered an error while running OUI, show the message containing the log
	# file information to users and then exit.
	#
	if [ $OUI_EXEC_STATUS -ne 0 -a -f $BOOTSTRAP_DIR/deinstallbootstrap.log ]
	then
		echo "Bootstrapping of the deinstall components is failed. Refer to $BOOTSTRAP_DIR/deinstallbootstrap.log file for more details"
		exit $OUI_EXEC_STATUS
	fi
	
	#
	# If OUI_EXEC_STATUS is not equal to zero, which means that OUI has encountered
        # some issues when bootstraping the components provided in orapram.ini.deinstall
        # file, and also, the log file is not present, exit.
	#
	if [ $OUI_EXEC_STATUS -ne 0 ]
	then
		echo "Bootstrapping of the deinstall components is failed."
		exit $OUI_EXEC_STATUS
	fi

	# Delete the log file if it exists, if OUI has bootstrapped comps and created the logfile.
	if [ -f $BOOTSTRAP_DIR/deinstallbootstrap.log ]
	then
		$RM $BOOTSTRAP_DIR/deinstallbootstrap.log
	fi

	#
	# Need to remove "-deinstall" parameter from command line arguments before
	# launching the deinstall tool which takes the same arguments list passed
	# to runInstaller. This is due to the fact that passing "-deinstall" flag to 
	# deinstall tool is redundant and also is not a valid argument to deinstall 
	# tool. 
	#
	TOOL_ARGS=`echo $@ | $SED -e "s/-deinstall//g"`
	#Move the required files from BOOTSTRAP_DIR/deinstall onto $BOOTSTRAP_DIR

	if [ -d $BOOTSTRAP_DIR/deinstall ]
	then
		$MV $BOOTSTRAP_DIR/deinstall $BOOTSTRAP_DIR/deinstall.$$
		$CP -rf $BOOTSTRAP_DIR/deinstall.$$/* $BOOTSTRAP_DIR/
		$RM -fr $BOOTSTRAP_DIR/deinstall.$$
	fi
	$BOOTSTRAP_DIR/deinstall $TOOL_ARGS
	DEINSTALL_EXEC_STATUS=$?
	# Clean up the boostrap area except "logs" if deinstall is failed
        if [ $DEINSTALL_EXEC_STATUS -ne 0 ]
        then
           cd $BOOTSTRAP_DIR
           find . -maxdepth 1 ! -name 'logs' ! -name '.*' | xargs rm -rf
        fi
	exit $DEINSTALL_EXEC_STATUS
fi


case "$BUNDLE" in
db)
# One-click: only if there is no cmd line argument and it's not on CRS node
if [ $# -gt 0 -o "$LOCAL" = "FALSE" -o \
     ! -r $CMDDIR/install/oneclick.properties ]; then
    $CMDDIR/install/.oui $* -J-Doracle.install.setup.workDir=$CWDDIR
else
    $CMDDIR/install/.oui -J-Doracle.install.setup.workDir=$CWDDIR
fi
;;
client)
    $CMDDIR/install/.oui $* CLUSTER_NODES={} -J-Doracle.install.setup.workDir=$CWDDIR
;;
crs)
    $CMDDIR/install/.oui $* -formCluster -J-Doracle.install.setup.workDir=$CWDDIR
;;
dv)
    $CMDDIR/install/.oui $* -J-Doracle.install.setup.workDir=$CWDDIR

;;
*)
    $CMDDIR/install/.oui $*
;;
esac

